﻿using AutoMapper;
using DotNetCore.CAP;
using Grpc.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Security.Claims;
using Windows.Admin.Contract;
using Windows.Admin.Contract.User.Model;
using Windows.Admin.Domain;
using Windows.Admin.Infrastructure.EFCore;
using Windows.Admin.Infrastructure.EFCore.Extensions;
using Windows.Event.Share;
using Windows.Infrastructure.Share.Extensions;
using Windows.Infrastructure.Share.Security;
using Windows.Infrastructure.Share.Tools;
using Windows.SeedWord;

namespace Windows.Admin.GrpcService
{
    public class UserGrpcService : IUserGrpcService
    {
        private readonly AdminDbContext _db;
        public UserGrpcService(AdminDbContext db)
        {
            _db = db;
        }

        public async Task<LoginResponse> SignInAsync(LoginRequest request)
        {
            var query = _db.User.Where(x => x.UserName == request.UserName);
            if (await query.CountAsync()<=0)
            {
                throw new RpcException(new Status(StatusCode.Internal, "用户名错误!"));
            }
            var user = await query.FirstOrDefaultAsync(x => x.Password == request.Password);
            if (user == null)
            {
                throw new RpcException(new Status(StatusCode.Internal, "密码错误!"));
            }
            LoginResponse response = new LoginResponse();
            response.Id = user.Id;
            response.UserName = user.UserName;
            return response;
        }
        public async Task<UserResponse> FindUserAsync(UserGrpcRequest request)
        {
            UserResponse response = new UserResponse();
            var user = await _db.User.Include(x => x.Employees).Include(x => x.Role_Users).FindByIdAsync(request.Id);
            response.Id = user.Id;
            response.UserName = user.UserName;
            response.IsEnable = user.IsEnable;
            if (user.Employees!.Count > 0)
            {
                var employee = user.Employees.FirstOrDefault();
                response.Name = employee!.Name;
                response.MobilePhone = employee.MobilePhone;
                response.Email = employee.Email;
            }
            else
            {
                response.Name = user.UserName;
            }
            if (user.Role_Users!.Count > 0)
            {
                if (user.Role_Users.Any(x => x.RoleId == 1))
                {
                    response.IsAdmin = true;
                }
                else
                {
                    //获取员工的组织机构
                    var organizationIds = user.Employees?.FirstOrDefault()?.Organization_Employees?.Select(s => s.OrganizationId).ToArray();
                    organizationIds ??= Array.Empty<long>();
                    //获取用户的角色Ids
                    var userRoleIds = await _db.Role.GetByUserId(user.Id).Select(s => s.Id).ToArrayAsync();
                    //获取员工的组织机构的角色Ids
                    var organizationRoleIds = await _db.Role.GetByOrganizationIds(organizationIds).Select(s => s.Id).ToArrayAsync();
                    //获取资源列表
                    long[] appIds = await _db.Privilege.GetByMasterValues(user.Id, organizationIds, userRoleIds, organizationRoleIds).Where(x => x.Access == AccessEnum.App.ToString()).Select(s => s.AccessValue).Distinct().ToArrayAsync();
                    var licenses = await _db.App.Where(x => appIds.Contains(x.Id) && x.License != null && x.License != "").Select(s => s.License!).ToArrayAsync();
                    response.Licenses = licenses;
                }
            }
            return response;
        }
    }
}
